package com.boguan.device.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.boguan.common.annotation.DataScope;
import com.boguan.common.constant.UserConstants;
import com.boguan.common.core.domain.entity.SysUser;
import com.boguan.common.exception.CustomException;
import com.boguan.common.utils.SecurityUtils;
import com.boguan.common.utils.StringUtils;
import com.boguan.device.domain.DeviceType;
import com.boguan.device.domain.DeviceTypeParam;
import com.boguan.device.mapper.DeviceTypeMapper;
import com.boguan.device.mapper.DeviceTypeParamMapper;
import com.boguan.device.service.IDeviceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * 设备类型逻辑处理实现类
 *
 * @author chen
 */
@Service
public class DeviceTypeServiceImpl implements IDeviceTypeService {

    @Autowired
    private DeviceTypeMapper deviceTypeMapper;

    @Autowired
    private DeviceTypeParamMapper deviceTypeParamMapper;

    @Override
    public int createDeviceType(DeviceType deviceType) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        deviceType.setCreateTime(new Date());
        deviceType.setCreateUserid(user.getUserId());
        deviceType.setUpdateTime(new Date());
        deviceType.setUpdateUserid(user.getUserId());
        return deviceTypeMapper.insertSelective(deviceType);
    }

    @Override
    public List<DeviceType> searchPage(DeviceType deviceType) {
        return deviceTypeMapper.selectPage(deviceType);
    }

    @Override
    public int updateDeviceType(DeviceType deviceType) {
        return deviceTypeMapper.updateByPrimaryKeySelective(deviceType);
    }

    @Override
    public int deleteDeviceType(Long typeId) {
        //查询设备类型
        int i = deviceTypeMapper.deleteByPrimaryKey(typeId);
        //查询组态数据
        deviceTypeParamMapper.deleteByTypeId(typeId);
        return i;
    }

    @Override
    public DeviceType selectDeviceTypeByTypeId(Long typeId) {
        return deviceTypeMapper.selectByPrimaryKey(typeId);
    }


    @Override
    public List<DeviceTypeParam> selectDeviceTypeParam(DeviceTypeParam deviceTypeParam) {
        if (StringUtils.isNull(deviceTypeParam.getTypeId())) {
            throw new CustomException("请输入设备类型id");
        }

        return deviceTypeParamMapper.selectByDeviceTypeParam(deviceTypeParam);
    }

    @Override
    public int createDeviceTypeParam(DeviceTypeParam deviceTypeParam) {
        return deviceTypeParamMapper.insertSelective(deviceTypeParam);
    }

    @Override
    public int updateDeviceTypeParam(DeviceTypeParam deviceTypeParam) {
        return deviceTypeParamMapper.updateByPrimaryKeySelective(deviceTypeParam);
    }

    @Override
    public int deleteDeviceTypeParam(Long paramId) {
        return deviceTypeParamMapper.deleteByPrimaryKey(paramId);
    }

    @Override
    public boolean checkModelNumber(Long typeId, String modelNumber) {
        DeviceType deviceType = selectDeviceTypeByTypeId(typeId);
        JSONArray jsonArray = JSONObject.parseArray(deviceType.getModelNumber());
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject model = jsonArray.getJSONObject(i);
            if (model.getString("value").equals(modelNumber)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public DeviceType selectTypeDetails(Long typeId) {
        DeviceType deviceType = deviceTypeMapper.selectByPrimaryKey(typeId);
        List jsonObject = JSONObject.parseObject(deviceType.getModelNumber(), List.class);
        deviceType.setModelNumberJson(jsonObject);
        return deviceType;
    }

    @Override
    public DeviceTypeParam selectDeviceTypeParamByParamId(Long paramId) {
        DeviceTypeParam deviceTypeParam = deviceTypeParamMapper.selectByPrimaryKey(paramId);
        if (StringUtils.isNull(deviceTypeParam)) {
            throw new CustomException("详情信息不存在");
        }
        List<Map<String, String>> list = new ArrayList<>();
        if (deviceTypeParam.getDataType() == 2) {
            String paramUnits = deviceTypeParam.getParamUnits();
            JSONObject jsonObject = JSONObject.parseObject(paramUnits);
            for (String key : jsonObject.keySet()) {
                Map<String, String> paramUnit = new HashMap<>();
                paramUnit.put("key", key);
                paramUnit.put("value", jsonObject.getString(key));
                list.add(paramUnit);
            }
            deviceTypeParam.setParamUnits(JSONObject.toJSONString(list));
        }
        return deviceTypeParam;
    }

    @Override
    public String checkIdentifier(DeviceTypeParam deviceTypeParam) {
        DeviceTypeParam checkIdentifier = deviceTypeParamMapper.checkIdentifier(deviceTypeParam.getIdentifier(), deviceTypeParam.getTypeId());
        if (StringUtils.isNull(checkIdentifier) || checkIdentifier.getParamId().equals(deviceTypeParam.getParamId())) {
            return UserConstants.UNIQUE;
        }
        return UserConstants.NOT_UNIQUE;
    }
}
